package com.night.icm.controller.base;

import com.night.icm.common.utils.PageDataDTO;
import com.night.icm.common.utils.SimpleDataDTO;
import com.night.icm.model.base.SystemDictionaryDTO;
import com.night.icm.model.base.SystemDictionaryValueDTO;
import com.night.icm.service.base.service.DictionaryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.util.List;

/**
 * DictionaryController
 *
 * @author Liao ZhiYong
 * @date 2019/12/22
 */
@Api(tags = {"字典管理"})
@RestController
@RequestMapping("/api/icm-dictionary")
public class DictionaryController {

    private final DictionaryService dictionaryService;

    @Autowired
    public DictionaryController(DictionaryService dictionaryService) {
        this.dictionaryService = dictionaryService;
    }

    @ApiOperation(value = "分页查询字典")
    @GetMapping(value = "")
    public PageDataDTO<SystemDictionaryDTO> findOnePage(
            @ApiParam(value = "页号，从0开始", required = true, defaultValue = "0") @RequestParam("page") @Min(0) int page,
            @ApiParam(value = "每页纪录条数", required = true, defaultValue = "20") @RequestParam("size") @Min(1) @Max(100) int size,
            @ApiParam(value = "排序字段, 例如：字段1,asc,字段2,desc") @RequestParam(value = "sort", required = false, defaultValue = "id,desc") String sort,
            @ApiParam(value = "字典编号") @RequestParam(value = "code", required = false) String code,
            @ApiParam(value = "字典名称") @RequestParam(value = "name", required = false) String name
    ) {
        return this.dictionaryService.findOnePage(page, size, sort, code, name);
    }

    @ApiOperation(value = "新建字典")
    @PostMapping(value = "")
    public SystemDictionaryDTO create(@ApiParam(value = "字典信息", required = true) @RequestBody SystemDictionaryDTO systemDictionaryDTO) {
        return this.dictionaryService.create(systemDictionaryDTO);
    }

    @ApiOperation(value = "删除字典")
    @DeleteMapping(value = "/{id}")
    public void delete(@ApiParam(value = "字典Id", required = true) @PathVariable(name = "id") String id) {
        this.dictionaryService.deleteById(id);
    }

    @ApiOperation(value = "更新字典")
    @PutMapping(value = "/{id}")
    public SystemDictionaryDTO update(
            @ApiParam(value = "字典Id", required = true) @PathVariable(name = "id") String id,
            @ApiParam(value = "字典信息", required = true) @RequestBody @Validated SystemDictionaryDTO systemDictionaryDTO
    ) {
        return this.dictionaryService.update(id, systemDictionaryDTO);
    }

    @ApiOperation(value = "查找所有字典")
    @GetMapping(value = "/all")
    public List<SystemDictionaryDTO> findAll() {
        return this.dictionaryService.findAll();
    }

    @ApiOperation(value = "根据字典编号查找信息, 通常用于下拉框数据查找, 无需权限")
    @GetMapping(value = "/code/{code}")
    public SystemDictionaryDTO findByCode(@ApiParam(value = "字典编号", required = true) @PathVariable String code,
                                          @ApiParam(value = "是否为全部值（包括available为false的值)", defaultValue = "false") @RequestParam(value = "all", defaultValue = "false") Boolean all) {
        return dictionaryService.findByCode(code, all);
    }

    @ApiOperation(value = "新建一个字典值")
    @PostMapping(value = "/{dictionaryId}/values")
    public SystemDictionaryValueDTO createValue(
            @ApiParam(value = "字典ID", required = true) @PathVariable String dictionaryId,
            @ApiParam(value = "字典值信息", required = true) @RequestBody @Validated SystemDictionaryValueDTO systemDictionaryValueDTO) {
        return dictionaryService.createValue(dictionaryId, systemDictionaryValueDTO);
    }

    @ApiOperation(value = "删除一个字典值")
    @DeleteMapping(value = "/{dictionaryId}/values/{valueId}")
    public void deleteValue(
            @ApiParam(value = "字典ID", required = true) @PathVariable String dictionaryId,
            @ApiParam(value = "字典值ID", required = true) @PathVariable String valueId) {
        dictionaryService.deleteValue(dictionaryId, valueId);
    }

    @ApiOperation(value = "修改一个字典值")
    @PutMapping(value = "/{dictionaryId}/values/{valueId}")
    public SystemDictionaryValueDTO updateValue(
            @ApiParam(value = "字典ID", required = true) @PathVariable String dictionaryId,
            @ApiParam(value = "字典值ID", required = true) @PathVariable String valueId,
            @ApiParam(value = "字典值", required = true) @RequestBody @Validated SystemDictionaryValueDTO systemDictionaryValueDTO) {
        return dictionaryService.updateValue(dictionaryId, valueId, systemDictionaryValueDTO);
    }

    @ApiOperation(value = "检查字典值Key是否唯一")
    @GetMapping(value = "/{dictionaryId}/values/key")
    public SimpleDataDTO<Boolean> checkDictionaryValue(@ApiParam(value = "字典ID", required = true) @PathVariable String dictionaryId,
                                                       @ApiParam(value = "字典值ID") @RequestParam(value = "dictionaryValueId", required = false) String dictionaryValueId,
                                                       @ApiParam(value = "字典值的key", required = true) @RequestParam(value = "key") String key) {
        return dictionaryService.checkDictionaryValue(dictionaryId, dictionaryValueId, key);
    }

    @ApiOperation(value = "检查字典Code是否唯一")
    @GetMapping(value = "/{dictionaryId}/code")
    public SimpleDataDTO<Boolean> checkDictionaryCode(@ApiParam(value = "字典ID") @PathVariable String dictionaryId,
                                                      @ApiParam(value = "字典值的code", required = true) @RequestParam(value = "code") String code) {
        return dictionaryService.checkDictionaryCode(dictionaryId, code);
    }
}